package com.platform.web.controller.base;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.*;
import com.platform.base.service.IBaseFactoryService;
import com.platform.base.service.IBaseLocationService;
import com.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.base.service.ITempInfoService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 温度采集器Controller
 * 
 * @author wangqi
 * @date 2020-09-26
 */
@Controller
@RequestMapping("/base/temp")
public class TempInfoController extends BaseController
{
    private String prefix = "base/temp";

    @Autowired
    private ITempInfoService tempInfoService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @RequiresPermissions("base:temp:view")
    @GetMapping()
    public String temp()
    {
        return prefix + "/temp";
    }

    /**
     * 查询温度采集器列表
     */
    @RequiresPermissions("base:temp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TempInfo tempInfo)
    {
        startPage();
        List<TempInfo> list = tempInfoService.selectTempInfoList(tempInfo);
        return getDataTable(list);
    }

    /**
     * @Author 王琦
     * @Description //温度采集器信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "温度采集器信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("base:temp:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TempInfo> util = new ExcelUtil<TempInfo>(TempInfo.class);
        List<TempInfo> infoList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = tempInfoService.importInfo(infoList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TempInfo> util = new ExcelUtil<TempInfo>(TempInfo.class);
        return util.importTemplateExcel("温度采集器信息");
    }

    /**
     * 导出温度采集器列表
     */
    @RequiresPermissions("base:temp:export")
    @Log(title = "温度采集器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TempInfo tempInfo)
    {
        List<TempInfo> list = tempInfoService.selectTempInfoList(tempInfo);
        ExcelUtil<TempInfo> util = new ExcelUtil<TempInfo>(TempInfo.class);
        return util.exportExcel(list, "温度采集器");
    }

    /**
     * 新增温度采集器
     */
    @GetMapping("/add/{locationId}")
    public String add(@PathVariable(value = "locationId") String locationId, ModelMap mmap)
    {
        mmap.put("location", baseLocationService.getById(locationId));
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/add";
    }

    /**
     * 新增保存温度采集器
     */
    @RequiresPermissions("base:temp:add")
    @Log(title = "温度采集器", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TempInfo tempInfo)
    {
        int count = tempInfoService.count(new QueryWrapper<TempInfo>().eq("location_id", tempInfo.getLocationId()));
        if(count>0){
            return AjaxResult.error("此地址下包含温度采集器");
        }
        tempInfo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(tempInfoService.insertTempInfo(tempInfo));
    }

    /**
     * 修改温度采集器
     */
    @GetMapping("/edit/{tempId}")
    public String edit(@PathVariable("tempId") String tempId, ModelMap mmap)
    {
        TempInfo baseTemp = tempInfoService.selectTempInfoById(tempId);
        mmap.put("baseTemp", baseTemp);
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/edit";
    }

    /**
     * 修改保存温度采集器
     */
    @RequiresPermissions("base:temp:edit")
    @Log(title = "温度采集器", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TempInfo tempInfo)
    {
        int count = tempInfoService.count(new QueryWrapper<TempInfo>().eq("location_id", tempInfo.getLocationId()));
        if(count>0){
            return AjaxResult.error("此地址下包含温度采集器");
        }
        tempInfo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(tempInfoService.updateTempInfo(tempInfo));
    }

    /**
     * 删除温度采集器
     */
    @RequiresPermissions("base:temp:remove")
    @Log(title = "温度采集器", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tempInfoService.deleteTempInfoByIds(ids));
    }

    /**
     * 校验温度采集器信息是否唯一
     */
    @PostMapping("/checkTempUnique")
    @ResponseBody
    public String checkTempUnique(TempInfo tempInfo)
    {
        return tempInfoService.checkTempUnique(tempInfo);
    }

    /**
     * 根据地址查询温度采集器信息
     */
    @PostMapping("/selectTempByLocation/{locationId}")
    @ResponseBody
    public String selectTempByLocation(@PathVariable("locationId") Long locationId)
    {
        TempInfo tempInfo = tempInfoService.getOne(new QueryWrapper<TempInfo>().eq("location_id", locationId));
        if(tempInfo != null){
            return tempInfo.getTempId();
        }
        return "";
    }
}
